<template>
  <div class="problem-container">
    <el-button type="primary" @click="handleAdd" class="add-btn">新增题目</el-button>
    
    <el-table :data="problemList" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="difficulty" label="难度" width="120" />
      <el-table-column prop="create_time" label="创建时间" width="180" />
      <el-table-column label="操作" width="300">
        <template #default="scope">
          <el-button size="small" @click="handleLike(scope.row)"><el-icon><StarFilled :class="{'text-yellow-500': scope.row.isLiked}" :style="{color: scope.row.isLiked ? '#eab308' : ''}" /></el-icon></el-button>
          <el-button size="small" @click="handleView(scope.row)">查看题解</el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="难度">
          <el-select v-model="form.difficulty" placeholder="请选择难度">
            <el-option label="简单" value="easy" />
            <el-option label="中等" value="medium" />
            <el-option label="困难" value="hard" />
          </el-select>
        </el-form-item>
        <el-form-item label="题目要求">
          <el-input v-model="form.requirement" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
    
    <!-- 查看题解对话框 -->
    <el-dialog v-model="solutionDialogVisible" title="题解" width="70%">
      <div style="white-space: pre-wrap;">{{ currentSolution }}</div>
      <template #footer>
        <el-button @click="solutionDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { StarFilled } from '@element-plus/icons-vue';
import {
  getProblemListService,
  addProblemService,
  updateProblemService,
  deleteProblemService,
  getProblemSolutionService
} from '@/api/problem';
import {ElMessage, ElMessageBox} from "element-plus";

const problemList = ref([]);
const loading = ref(false);

onMounted(() => {
  fetchProblems();
});

// 获取题目列表
const fetchProblems = async () => {
  try {
    loading.value = true;
    const res = await getProblemListService();
    problemList.value = res.data;
  } catch (error) {
    ElMessage.error('获取题目列表失败');
  } finally {
    loading.value = false;
  }
};

const dialogVisible = ref(false);
const dialogTitle = ref('');
const form = ref({
  id: '',
  title: '',
  difficulty: '',
  requirement: '',
  content: ''
});

const handleAdd = () => {
  dialogTitle.value = '新增题目';
  form.value = { id: '', title: '', difficulty: '', requirement: '', content: '' };
  dialogVisible.value = true;
};

const handleEdit = (row) => {
  dialogTitle.value = '编辑题目';
  form.value = { ...row };
  dialogVisible.value = true;
};

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该题目吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteProblemService(row.id);
      problemList.value = problemList.value.filter(item => item.id !== row.id);
      ElMessage.success('删除成功');
    } catch (error) {
      ElMessage.error('删除失败');
    }
  }).catch(() => {});
};

const solutionDialogVisible = ref(false);
const currentSolution = ref('');

const handleLike = (row) => {
  if (!row.likeCount) {
    row.likeCount = 0;
  }
  row.likeCount++;
  row.isLiked = !row.isLiked;
  ElMessage.success('点赞成功');
};

const handleView = async (row) => {
  try {
    const res = await getProblemSolutionService(row.id);
    currentSolution.value = res.data;
    solutionDialogVisible.value = true;
  } catch (error) {
    ElMessage.error('获取题解失败');
  }
};

const submitForm = async () => {
  try {
    if (form.value.id) {
      // 编辑逻辑
      await updateProblemService(form.value);
      const index = problemList.value.findIndex(item => item.id === form.value.id);
      if (index !== -1) {
        problemList.value[index] = { ...form.value };
      }
    } else {
      // 新增逻辑
      const res = await addProblemService(form.value);
      problemList.value.push({
        ...form.value,
        id: res.data.id,
        createTime: new Date().toLocaleDateString()
      });
    }
    dialogVisible.value = false;
    ElMessage.success('操作成功');
  } catch (error) {
    ElMessage.error('操作失败');
  }
};
</script>

<style scoped>
.problem-container {
  padding: 20px;
}
.add-btn {
  margin-bottom: 20px;
}
</style>